package com.wxproject.demo.Utils;/*
 *项目名: WXTEST
 *文件名: EmailUtil
 *创建者: ZQF
 *创建时间:18/01/2021 17:41
 *描述: TODO
 */

import com.wxproject.demo.Common.lang.ResultMap;
import com.wxproject.demo.Service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class EmailUtil {
    private String url = "https://wwsszz.top/#/";
    private String vurl = "https://wwsszz.top/#/";

    @Autowired
    private LoginService loginService;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private ResultMap resultMap;


    public Integer SendEmail(String email,Long id){
        Integer hasEmail = loginService.checkEmail(email);
        Map<Object,Object> res = new HashMap<>();
        Long active = SnowFlake.nextId();
        RedisUtil.set(String.valueOf(active),id,1800);
        String code = RandowcodeUtil.getCheckCode();
//        redisUtil.set("Registercode-"+code+"-"+email,code,600);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("管理员"+'<'+"a812257301@sina.com"+'>');
        message.setTo(email);
        message.setSubject("账户激活");
        message.setText("点击连接来激活您的账户"+"  "+url+"active?active="+active);
        javaMailSender.send(message);
        return 1;
    }


    public Integer changePassword(String email, String id){
        System.out.println(email);
        SimpleMailMessage message = new SimpleMailMessage();
        Integer isExtence = loginService.extenceEmail(email);
        String ans = loginService.getEmailByUsername(email);
        System.out.println(isExtence+"  "+ans);
        if (isExtence!=null&&isExtence==0){
            RedisUtil.set("changePasswordId-" + id, email, 900);
            message.setTo(email);
            message.setFrom("管理员"+'<'+"a812257301@sina.com"+'>');
            message.setSubject("修改密码");
            message.setText("点击修改您的账户密码"+"  "+vurl+"index?codeId="+id);
            javaMailSender.send(message);
        }else if (ans!=null){
            RedisUtil.set("changePasswordId-" + id, ans, 900);
            message.setTo(ans);
            message.setFrom("a812257301@sina.com");
            message.setSubject("修改密码");
            message.setText("点击修改您的账户密码"+"  "+vurl+"index?codeId="+id);
            javaMailSender.send(message);
        }else{
            return null;
        }
        return 1;
    }
}
